package com.ld.security.tool;

import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpUtil;
import com.ld.security.common.core.util.FileUtil;

import java.io.File;

/**
 * 远程加载类
 */
public class FileClassLoader extends ClassLoader {

    private String rootPath ;

    public FileClassLoader(String rootPath) {
        super();
        this.rootPath = rootPath;
    }

    public Class<?> fileLoadClass(String name) {
        File f =  new File(rootPath+name);

        byte[] bs = FileUtil.readBytes(f);
        Console.log(f.getName());

        String fileName = name;
        String className = fileName.replace( FileUtil.getSuffix(fileName),"");
        className =  className.replace("/",".");


        Class<?> cla =  this.defineClass(className,bs,0,bs.length);
        return cla;
    }

}
